<?php
/**
 * 金额导入模型
 * @author jianhui
 * 
 * type(1:百城名单,2：双月额度)
 *
 */
class importMoneyModel extends MY_Model{
    function importMoneyModel(){
        parent::MY_Model();
        $this->table = 'importMoney';
    }
    /**
     * 导入百城数据
     * @param array $data
     */
    function add100CityData($data){
        if (!isset($data['year']) || !$data['year']){
            return false;
        }
        if(!isset($data['timeUnit']) || !$data['timeUnit']){
            return false;
        }
        if(!isset($data['coding']) || !$data['coding']){
            return false;
        }
        $this->load->model('sellpointModel');
        $storeData = $this->sellpointModel->getNewData(array('Coding'=>$data['coding'], 'SpState_sys'=>1));
        if (!$storeData){
            return false;
        }
        $where = array(
            'storeId' => (int)$storeData['userId'],
            'timeUnit' => (int)$data['timeUnit'],
            'year' => (int)$data['year'],
            'type' => 1,
        );
        $updata = array(
            'area' => $storeData['area'],
            'region' => $storeData['region'],
            'spsarea' => $storeData['spsarea'],
            'city' => $storeData['city'],
            'coding' => $storeData['coding'],
            'storeName' => $storeData['storeName'],
            'money' => 0,
            'createTime' => time()
        );
        return $this->upset($where, $updata);
    }
    /**
     * 导入季度额度
     * @param array $data
     */
    function addTimeUnitData($data){
        if (!isset($data['year']) || !$data['year']){
            return false;
        }
        if(!isset($data['timeUnit']) || !$data['timeUnit']){
            return false;
        }
        if(!isset($data['coding']) || !$data['coding']){
            return false;
        }
        /* if (!isset($data['money']) || !$data['money']){
            return false;
        } */
        $this->load->model('sellpointModel');
        $storeData = $this->sellpointModel->getNewData(array('Coding'=>$data['coding'], 'SpState_sys'=>1));
        if (!$storeData){
            echo "找不到专营店编码".$data['coding']."的数据";
            return false;
        }
        $where = array(
            'storeId' => (int)$storeData['userId'],
            'timeUnit' => (int)$data['timeUnit'],
            'year' => (int)$data['year'],
            'type' => 2,
        );
        $updata = array(
            'area' => $storeData['area'],
            'region' => $storeData['region'],
            'spsarea' => $storeData['spsarea'],
            'city' => $storeData['city'],
            'coding' => $storeData['coding'],
            'storeName' => $storeData['storeName'],
            'money' => $data['money'],
            'createTime' => time()
        );
        $insertId = $this->upset($where, $updata);
        if ($insertId){
            $this->load->model('singleProcessModel');
            $procId = $this->singleProcessModel->initData(array('storeId'=>$where['storeId'],'timeUnit'=>$where['timeUnit'],'year'=>$where['year']));
            //更新金额
            $this->singleProcessModel->updateMoney($procId);
        }
        return $insertId;
    }
    /**
     * 获取指定导入类别金额
     * @param int $storeId 店铺用户ID
     * @param int $timeUnit 时间单位
     * @param int $type 金额类别
     * @param number $year 年份
     * @return number
     */
    function getMoney($storeId, $timeUnit, $type , $year = 2015){
        $findData = $this->getData(array('storeId'=>$storeId, 'timeUnit'=>$timeUnit, 'year'=>$year, 'type'=>$type));
        return isset($findData) && $findData['money'] ? (float)$findData['money'] : 0;
    }
    /**
     * 获取主担店ID
     */
    function getMainStoreId($city, $year = 2015){
        $findData = $this->getData(array('city'=>$city,'type'=>1, 'year'=>$year));
        return isset($findData) && $findData['storeId'] ? (int)$findData['storeId'] : 0;
    }
}